package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.MemberConfig;
import com.rm.zeyou.entity.UserMember;
import com.rm.zeyou.mapper.MemberConfigMapper;
import com.rm.zeyou.mapper.UserMemberMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.MemberConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.compute.ArithmeticUtil;
import com.rm.zeyou.vo.response.SelectMemberResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员配置表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:08
 */
@Service
public class MemberConfigServiceImpl extends ServiceImpl<MemberConfigMapper, MemberConfig> implements MemberConfigService {
    @Resource
    private MemberConfigMapper memberConfigMapper;
    @Resource
    private UserMemberMapper userMemberMapper;

    @Override
    public ApiRestResponse<SelectMemberResponseVo> selectMemberList(Long userId) {
        SelectMemberResponseVo responseVo = new SelectMemberResponseVo();
        //查自己是否是会员
        UserMember userMember = userMemberMapper.selectOne(new LambdaQueryWrapper<UserMember>().eq(UserMember::getUserId, userId));

        if (!CheckEmptyUtil.isEmpty(userMember)) {
            responseVo.setIsMember(NumberConstants.ONE_CONSTANTS);
            responseVo.setMemberExpirationDate(userMember.getExpiryTime());
        } else {
            responseVo.setIsMember(NumberConstants.ZERO_CONSTANTS);
        }
        //会员列表
        List<MemberConfig> memberList = memberConfigMapper.selectList(new LambdaQueryWrapper<MemberConfig>()
                .orderByAsc(MemberConfig::getMemberDiscountPrice));
        List<SelectMemberResponseVo.Member> voList = new ArrayList<>();
        if (!CheckEmptyUtil.isEmpty(memberList)) {
            memberList.forEach(item -> {
                SelectMemberResponseVo.Member vo = new SelectMemberResponseVo.Member();
                vo.setId(item.getId());
                vo.setMemberName(item.getMemberName());
                vo.setMemberConservePrice(ArithmeticUtil.sub(item.getMemberPrice(), item.getMemberDiscountPrice()));
                vo.setMemberPrice(item.getMemberPrice());
                vo.setMemberDiscountPrice(item.getMemberDiscountPrice());

                voList.add(vo);
            });
        }
        responseVo.setMemberList(voList);
        return ApiRestResponse.success(responseVo);

    }
}
